<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController
{
	/**
	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout='//layouts/main';
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu=array();
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();

    //火综退保失败的返回报文格式
    public function huoSurrenderError($content,$end=true) {
        echo '{"waterNo":"","policyNo":"","retCode":"11","returnMsg":"'.$content.'"}';
        if($end)die;
    }

    //航综退保失败的返回报文格式
    public function hangSurrenderError($content,$end=true) {
        $xml_content = '<?xml version="1.0" encoding="UTF-8"?>
                <CancelResponse>
                    <CWaterNo></CWaterNo>
                    <policyNum></policyNum>
                    <RetCode>11</RetCode>
                    <ReturnMsg>'.$content.'</ReturnMsg>
                    <Policy_status></Policy_status>
                    <RealDate></RealDate>
                </CancelResponse>
                ';
        echo $xml_content;
        if($end)die;
    }

    //火综承保失败的返回报文格式
    public function huoInsureError($content,$end=true) {
        echo '{"waterNo":"","retCode":"11","retDesc":"'.$content.'","policyNum":"","transTime":""}';
        if($end)die;
    }

    //航综承保报文的报错报文格式
    public function hangInsureError($content,$end=true) {
        $xml_content = '<?xml version="1.0" encoding="UTF-8"?>
            <ApplyResponse>
                <CWaterNo></CWaterNo>
                <endDate></endDate>
                <policyNum></policyNum>
                <retCode>11</retCode>
                <retDesc>'.$content.'</retDesc>
                <startDate></startDate>
                <policyStatus></policyStatus>
                <RealDate></RealDate>
                <strLockKey></strLockKey>
                <company></company>
            </ApplyResponse>
            ';
        echo $xml_content;
        if($end)die;
    }

    // 解决json中文乱码
    public function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
    {
        static $recursive_counter = 0;
        if (++$recursive_counter > 1000) {
            die('possible deep recursion attack');
        }
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                arrayRecursive($array[$key], $function, $apply_to_keys_also);
            } else {
                $array[$key] = $function($value);
            }

            if ($apply_to_keys_also && is_string($key)) {
                $new_key = $function($key);
                if ($new_key != $key) {
                    $array[$new_key] = $array[$key];
                    unset($array[$key]);
                }
            }
        }
        $recursive_counter--;
    }
    public function JSON($array) {
        $this-> arrayRecursive($array, 'urlencode', true);
        $json = json_encode($array);
        return urldecode($json);
    }
    //判断是否为身份证
    public function idcard($card) {
        $card = $this->to18Card($card);
        if (strlen($card) != 18) {
            return false;
        }
        $cardBase = substr($card, 0, 17);
        return ($this->getVerifyNum($cardBase) == strtoupper(substr($card, 17, 1)));
    }
    //格式化15位身份证号码为18位
    public function to18Card($card) {
        $card = trim($card);
        if (strlen($card) == 18) {
            return $card;
        }
        if (strlen($card) != 15) {
            return false;
        }
        // 如果身份证顺序码是996 997 998 999，这些是为百岁以上老人的特殊编码
        if (array_search(substr($card, 12, 3), array('996', '997', '998', '999')) !== false) {
            $card = substr($card, 0, 6).'18'.substr($card, 6, 9);
        } else {
            $card = substr($card, 0, 6).'19'.substr($card, 6, 9);
        }
        $card = $card.$this->getVerifyNum($card);
        return $card;
    }
    // 计算身份证校验码，根据国家标准gb 11643-1999
    public function getVerifyNum($cardBase) {
        if (strlen($cardBase) != 17) {
            return false;
        }
        // 加权因子
        $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
        // 校验码对应值
        $verify_number_list = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
        $checksum = 0;
        for ($i = 0; $i < strlen($cardBase); $i++) {
            $checksum += substr($cardBase, $i, 1) * $factor[$i];
        }
        $mod = $checksum % 11;
        $verify_number = $verify_number_list[$mod];
        return $verify_number;
    }

    //生日和性别提取
    public function sexIDCard($IDCard) {
        if (strlen($IDCard) == 18) {
            return $this->check18sex($IDCard);
        } elseif ((strlen($IDCard) == 15)) {
            return $this->check15sex($IDCard);
        } else {
            return false;
        }
    }
    public function check18sex($IDCard) {
        $num = intval(substr($IDCard,16,1));
        if(($num%2) == 0) {
            return "02";
        } else {
            return "01";
        }
    }
    public function check15sex($IDCard) {
        $num = intval(substr($IDCard,14,1));
        if(($num%2) == 0) {
            return "02";
        } else {
            return "01";
        }
    }

    public function birthIDCard($IDCard) {
        if (strlen($IDCard) == 18) {
            return $this->check18birthday($IDCard);
        } elseif ((strlen($IDCard) == 15)) {
            $IDCard = $this->convertIDCard15to18($IDCard);
            return $this->check18birthday($IDCard);
        } else {
            return false;
        }
    }

    public function check18birthday($IDCard) {
        $tyear= substr($IDCard,6,4);
        $tmonth= substr($IDCard,10,2);
        $tday= substr($IDCard,12,2);
        return $tyear."-".$tmonth."-".$tday;
    }

    public function convertIDCard15to18($IDCard) {
        if (strlen($IDCard) != 15) {
            return false;
        } else {
            // 如果身份证顺序码是996 997 998 999，这些是为百岁以上老人的特殊编码
            if (array_search(substr($IDCard, 12, 3), array('996', '997', '998', '999')) !== false) {
                $IDCard = substr($IDCard, 0, 6) . '18' . substr($IDCard, 6, 9);
            } else {
                $IDCard = substr($IDCard, 0, 6) . '19' . substr($IDCard, 6, 9);
            }
        }
        $IDCard = $IDCard . $this->calcIDCardCode($IDCard);
        return $IDCard;
    }

    public function request_by_curl($remote_server,$post_string,$ssl=false){
        if(function_exists('curl_init')){
            $ch = curl_init();
            $header[] = "Content-type: text/xml";//定义content-type为xml
            curl_setopt($ch,CURLOPT_URL,$remote_server);
//             curl_setopt($ch, CURLOPT_TIMEOUT, 5);
            curl_setopt($ch,CURLOPT_POST, 1);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$post_string);
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
            if($ssl){
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            }
            $data = curl_exec($ch);
            curl_close($ch);
            return $data;
        }else{
            return '请先安装curl';
        }
    }

    function curl_https($url, $data=array(), $header=array(), $timeout=15){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        $response = curl_exec($ch);
        if($error=curl_error($ch)){
            die($error);
        }
        curl_close($ch);
        return $response;
    }

}